package com.abhinav.designPatterns.singleton;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.ObjectStreamException;
import java.io.Serializable;

public class SingletonClass implements Serializable{
	
	private static final long serialVersionUID = 6986114661728108842L;
	private volatile static SingletonClass _instance;
	private transient double i = 0;
	
	private SingletonClass() {
		System.out.println("Calling SingletonClass constructor");
		i = Math.random();
	}

	public static SingletonClass getInstance(){
		if(_instance == null){
			synchronized (SingletonClass.class) {
				if(_instance == null){
					_instance = new SingletonClass();
				}
			}
		}
		return _instance;
	}

	@Override
	public String toString() {
		return "SingletonClass: "+i ;
	}
	
	public Object readResolve() throws ObjectStreamException {
		return _instance;
	}
	
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return _instance;
	}
	
	public void setI(double val){
		i = val;
	}

}
